/**
package com.developteam.xnmall.service.job;

import com.github.wxpay.sdk.WXPay;
import com.developteam.xnmall.dao.OrderItemMapper;
import com.developteam.xnmall.dao.OrdersMapper;
import com.developteam.xnmall.dao.ProductSkuMapper;
import com.developteam.xnmall.entity.OrderItem;
import com.developteam.xnmall.entity.Orders;
import com.developteam.xnmall.entity.ProductSku;
import com.developteam.xnmall.service.OrderService;
import com.developteam.xnmall.service.impl.OrderServiceImpl;
import com.developteam.xnmall.service.job.MyPayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class OrderTimeoutCheckJob {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderService orderService;
/*    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;

    private WXPay wxPay = new WXPay(new MyPayConfig());

//    @Scheduled(cron = "0/60 * * * * ?")
    //不需要定时任务了, 要监听MQ

    public void checkAndCloseOrder() {
        System.out.println("-------------------查询是否购买");
        try {
            //1.查询超过30min订单状态依然为待支付状态的订单
            Example example = new Example(Orders.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("status", "1");
            Date time = new Date(System.currentTimeMillis() - 30 * 60 * 1000);
            criteria.andLessThan("createTime", time);
            List<Orders> orders = ordersMapper.selectByExample(example);    //只是可能超时的订单

            //2.访问微信平台接口，确认当前订单最终的支付状态
            for (int i = 0; i < orders.size(); i++) {
                Orders order = orders.get(i);
                HashMap<String, String> params = new HashMap<>();
                params.put("out_trade_no", order.getOrderId());  //具体订单号

                Map<String, String> resp = wxPay.orderQuery(params);   //得到的resp是订单的状态

                if("SUCCESS".equalsIgnoreCase(resp.get("trade_state"))){
                    //2.1 如果订单已经支付，则修改订单状态为"代发货/已支付"  status = 2
                    Orders updateOrder = new Orders();
                    updateOrder.setOrderId(order.getOrderId());
                    updateOrder.setStatus("2");
                    ordersMapper.updateByPrimaryKeySelective(updateOrder);
                }else if("NOTPAY".equalsIgnoreCase(resp.get("trade_state"))){
                    //2.2 如果确实未支付 则取消订单：
                    //  a.向微信支付平台发送请求，关闭当前订单的支付链接
                    Map<String, String> map = wxPay.closeOrder(params);
                    System.out.println(map);

                    // b.关闭订单
                    orderService.closeOrder(order.getOrderId());

    //要让这两步操作保证原子性【一起成功，一起失败】，一致性【关闭订单那库存就一定要还原，否则数据库不一致】
                    //  1.修改当前订单：status=6 已关闭  close_type=1 超时未支付
                    /*Orders cancleOrder = new Orders();
                    cancleOrder.setOrderId(order.getOrderId());
                    cancleOrder.setStatus("6");    //已关闭
                    cancleOrder.setCloseType(1);   //关闭类型，超时未支付
                    ordersMapper.updateByPrimaryKeySelective(cancleOrder);

                    // c.还原库存，根据当前订单编号查询商品快照--》修改product_sku
/*                    Example example1 = new Example(OrderItem.class);
                    Example.Criteria criteria1 = example1.createCriteria();
                    criteria1.andEqualTo("orderId", order.getOrderId());
                    List<OrderItem> orderItems = orderItemMapper.selectByExample(example1);   //就是已取消的订单快照
                //还原库存  订单快照一次还原
                    for (int j = 0; j < orderItems.size() ; j++) {
                        OrderItem orderItem = orderItems.get(j);
                        //修改
                        ProductSku productSku = productSkuMapper.selectByPrimaryKey(orderItem.getSkuId());
                        productSku.setStock(productSku.getStock() + orderItem.getBuyCounts());
                        productSkuMapper.updateByPrimaryKey(productSku);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
**/